package com.badibadi.mytools;

import android.view.View;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ImageView;

public class ViewTools {
	 int ViewW=0;
	/**
	 * 获取view的宽 这是方法一
	 */
	public static int getViewWidth1(View imageView)
	{
		
	      //------------------------------------------------方法一  
	      int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);  
	      int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);  
	      imageView.measure(w, h);  
	      int height =imageView.getMeasuredHeight();  
	      int width =imageView.getMeasuredWidth();  

		return width;
		//方法2
//		ViewTreeObserver vto = imageView.getViewTreeObserver();  
//		vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {  
//		    public boolean onPreDraw() {  
//		        int height = imageView.getMeasuredHeight();  
//		        int width = imageView.getMeasuredWidth();  
//		        textView.append("\n"+height+","+width);  
//		        return true;  
//		    }  
//		});  
		
		//方法3
//		ViewTreeObserver vto2 = imageView.getViewTreeObserver();    
//		vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  
//		    @Override    
//		    public void onGlobalLayout() {  
//		        imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);    
//		        textView.append("\n\n"+imageView.getHeight()+","+imageView.getWidth());  
//		    }    
//		});    
	}
	
	
	public int getViewWidth3(final View imageView)
	{
		
	    //-----------------------------------------------方法三     
	    ViewTreeObserver vto2 = imageView.getViewTreeObserver();    
	    vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  
	        @Override    
	        public void onGlobalLayout() {  
	            imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);    
//	            ViewW=imageView.getWidth();
	            ViewW=imageView.getWidth();
//	            textView.append("\n\n"+imageView.getHeight()+","+imageView.getWidth());  
	        }    
	    });
		return ViewW;
	}
	
	
	
}
